package io.github.wslxm.springbootplus2.manage.sys.handle;

import com.google.common.base.CaseFormat;
import io.github.wslxm.springbootplus2.core.enums.BaseDic;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.core.utils.XjStringUtil;
import io.github.wslxm.springbootplus2.core.utils.tree.XjTreeUtil;
import io.github.wslxm.springbootplus2.core.utils.validated.XjValidUtil;
import io.github.wslxm.springbootplus2.manage.sys.dao.SysDictDao;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysDict;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysDictVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * 字典功能 枚举生成处理器
  * @author 王松
  * @mail  1720696548@qq.com
  * @date  2025/9/23 9:16
  */
@Service
@Slf4j
public class SysDictServiceHandle  {

    @Autowired
    private SysDictDao sysDictDao;

    public Map<String, String> generateEnum(String pid) {
        List<SysDict> list = sysDictDao.list();
        List<SysDictVO> dictionaryVos = XjBeanUtil.listVo(list, SysDictVO.class);
        SysDictVO dictionaryVO = XjTreeUtil.nextTree(dictionaryVos, pid);
        Map<String, String> map = new HashMap<>(2, 1);
        if (dictionaryVO.getType().equals(BaseDic.DictType.V1.getValue())) {
            // 生成小模块的枚举类
            String enumsJava = this.generateEnumJavaByTypeDirectory(dictionaryVO);
            map.put("java", enumsJava);
        } else if (dictionaryVO.getType().equals(BaseDic.DictType.V2.getValue())) {
            // 生成单个字典的枚举类
            String enumsJava = this.generateEnumJavaByTypeDict(dictionaryVO);
            map.put("java", enumsJava);
        }
        // 2、生成js key
        SysDictVO sysDictVO = XjTreeUtil.nextTree(dictionaryVos, "0");
        map.put("js", this.generateEnumJs(sysDictVO));
        return map;
    }


    /**
     * 生成java 代码枚举对象， 请将生成好的代码直接替换到 enums/Enums 小的二级类(指定为2级，每个子模块枚举一个类)
     *
     * @author wangsong
     * @email 1720696548@qq.com
     * @date 2020/8/16 0016 0:10
     * @version 1.0.1
     */
    private String generateEnumJavaByTypeDirectory(SysDictVO dict) {
        String code = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, dict.getCode());
        StringBuilder sb = new StringBuilder();
        sb.append("\nimport com.baomidou.mybatisplus.annotation.IEnum;");
        sb.append("\nimport lombok.AllArgsConstructor;");
        sb.append("\nimport lombok.Getter;\n");
        sb.append("\npublic interface ").append(code).append("{\n");
        // 字段名
        for (SysDictVO dictField : dict.getChildren()) {
            //枚举字典的-枚举属性
            if (dictField.getChildren() == null) {
                continue;
            }
            String mode = XjStringUtil.isInteger(dictField.getChildren().get(0).getCode()) ? "Integer" : "String";
            String moduleName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, dictField.getCode());
            sb.append("\n        /**");
            sb.append("\n         * ").append(dictField.getDesc());
            sb.append("\n         */");
            sb.append("\n        @Getter");
            sb.append("\n        @AllArgsConstructor");
            sb.append("\n        enum ").append(moduleName).append(" implements IEnum<" + mode + "> {");
            for (SysDictVO dictValue : dictField.getChildren()) {
                if (XjStringUtil.isInteger(dictValue.getCode())) {
                    sb.append("\n            V").append(dictValue.getCode()).append("(").append(dictValue.getCode())
                            .append(", \"").append(dictValue.getName()).append("\"),");
                    sb.append("    // ").append(dictValue.getDesc());
                } else {
                    // 变更为驼峰模式
                    String codeStr = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE, dictValue.getCode().toUpperCase());
                    codeStr = codeStr.replace("-", "_");
                    sb.append("\n            ").append(codeStr).append("(\"").append(dictValue.getCode())
                            .append("\", \"").append(dictValue.getName()).append("\"),");
                    sb.append("    // ").append(dictValue.getDesc());
                }
            }
            sb.append("\n            ;\n");
            sb.append("            private ").append(mode).append(" value;\n");
            sb.append("            private String desc;\n");
            sb.append("        }\n");
        }
        sb.append("    }\n");
        return sb.toString();
    }

    /**
     * @param dict
     * @return
     */
    private String generateEnumJavaByTypeDict(SysDictVO dict) {
        //枚举字典的-枚举属性 没有下级数据
        XjValidUtil.isTrue(dict.getChildren() == null || dict.getChildren().size() == 0, "dict [" + dict.getCode() + "] there is no sub-level data");
        StringBuilder sb = new StringBuilder();
        sb.append("\nimport com.baomidou.mybatisplus.annotation.IEnum;");
        sb.append("\nimport lombok.AllArgsConstructor;");
        sb.append("\nimport lombok.Getter;\n");
        // 字段名
        String mode = XjStringUtil.isInteger(dict.getChildren().get(0).getCode()) ? "Integer" : "String";
        String moduleName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, dict.getCode());
        sb.append("\n/**");
        sb.append("\n * ").append(dict.getDesc());
        sb.append("\n */");
        sb.append("\n@Getter");
        sb.append("\n@AllArgsConstructor");
        sb.append("\nenum ").append(moduleName).append(" implements IEnum<" + mode + "> {");

        for (SysDictVO dictValue : dict.getChildren()) {
            if (XjStringUtil.isInteger(dictValue.getCode())) {
                sb.append("\n    V").append(dictValue.getCode()).append("(").append(dictValue.getCode())
                        .append(", \"").append(dictValue.getName()).append("\"),");
                sb.append("  // ").append(dictValue.getDesc());
            } else {
                // 变更为驼峰模式
                String codeStr = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE, dictValue.getCode().toUpperCase());
                codeStr = codeStr.replace("-", "_");
                sb.append("\n    ").append(codeStr).append("(\"").append(dictValue.getCode())
                        .append("\", \"").append(dictValue.getName()).append("\"),");
                sb.append("  // ").append(dictValue.getDesc());
            }
        }
        sb.append("\n    ;\n");
        sb.append("    private ").append(mode).append(" value;\n");
        sb.append("    private String desc;\n");
        sb.append("}\n");
        return sb.toString();
    }


    /**
     * 拼接js 字典数据-KEY
     *
     * @author wangsong
     * @mail 1720696548@qq.com
     * @date 2020/8/16 0016 0:29
     * @version 1.0.1
     */
    private String generateEnumJs(SysDictVO dict) {
        StringBuilder sb = new StringBuilder();
        //
        sb.append("var Enums = {");
        //模块名
        for (SysDictVO dictModule : dict.getChildren()) {
            sb.append("\n    // ").append(dictModule.getName());
            sb.append("\n    ").append(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, dictModule.getCode() + ": {"));
            //枚举字典的-枚举名--驼峰模式
            for (SysDictVO dictField : dictModule.getChildren()) {
                String moduleName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, dictField.getCode());
                sb.append("\n        ").append(moduleName).append(" : \"").append(dictField.getCode()).append("\",  // ").append(dictField.getName());
            }
            sb.append("\n    },");
        }
        sb.append("\n};");
        return sb.toString();
    }
}

